﻿// ***********************************************************************
// Assembly         : OSS.BIM
// Author           : tian_
// Created          : 09-04-2018
//
// Last Modified By : tian_
// Last Modified On : 09-04-2018
// ***********************************************************************
// <copyright file="ProjectEditCmd.cs" company="北京智帆高科科技有限公司">
//     Copyright © 北京智帆高科科技有限公司 2018
// </copyright>
// <summary></summary>
// ***********************************************************************
using System.Windows.Forms;
using OSS.BIM.Business.Utility;
using OSS.BIM.Model;
using OSS.BIM.UI.Forms;
using OSS.BIM.Utility;
using ZFGK.Addins;
using ZFGK.osgEarth.UI;
using ZFGK.Utility;

namespace OSS.BIM.Commands.ProjcetCmds
{
    /// <summary>
    /// Class ProjectEditCmd.
    /// </summary>
    /// <seealso cref="ZFGK.Addins.IExtendCommand" />
    public class ProjectEditCmd : IExtendCommand
    {
        /// <summary>
        /// Executes the specified command data.
        /// </summary>
        /// <param name="cmdData">The command data.</param>
        /// <param name="message">The message.</param>
        /// <returns>CmdResult.</returns>
        public CmdResult Execute(ExtendCmdData cmdData, ref string message)
        {
            var mainForm = cmdData.MainForm as Form;
            var osgEarthViewForm = cmdData.ViewForm as OsgEarthViewForm;
            if (osgEarthViewForm == null)
                return CmdResult.Failure;
            var osgEarthViewCtrl = osgEarthViewForm.OsgEarthViewCtrl;
            var osgEarthObj = osgEarthViewCtrl.OsgEarthObj;
            var actionUtility = osgEarthViewCtrl.ActionUtility;

            var project = cmdData.Tag as Project;
            if (project == null)
                return CmdResult.Cancel;

            var projectForm = new ProjectForm(project);
            projectForm.Icon = mainForm.Icon;
            if (projectForm.ShowDialog(mainForm) != DialogResult.OK)
                return CmdResult.Cancel;

            if (!ProjectDbUtil.Update(project))
            {
                ZfMessageUtil.ShowError("添加到数据库失败！");
            }

            project.AddOrUpdateTreeListNode();
            foreach (var modelGrope in project.GetModelGropes())
            {
                osgEarthObj.AddOrReplaceGroup(modelGrope);
            }

            return CmdResult.Succeed;

        }
    }
}
